package net.huashitong.Indicators.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.entity.UserInfo;
import net.huashitong.Indicators.service.UserInfoService;
import net.huashitong.Indicators.utils.TokenUtil;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class TokenTimeInterceptor implements HandlerInterceptor {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        // Map<String,String[] > openId= request.getParameterMap();
        String openId = request.getParameter("openId");
        UserInfo user = userInfoService.getById(openId);
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "309");
            map.put("message", "无用户");
            map.put("data", "无数据");
            PrintWriter writer = response.getWriter();
            writer.print(JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteDateUseDateFormat));
            writer.close();
            response.flushBuffer();
            return false;
        }
        String currenttime = Until.getCurrentDateTime();
        if (TokenUtil.validateToken(user)) {
            user.setLocalTokenTime(currenttime);
            userInfoService.update(user);
            return true;
        } else {
            // TokenUtil util = new TokenUtil();

            Map<String, Object> map = new HashMap<>();
            user.setLocalToken(TokenUtil.makeToken());
            user.setLocalTokenTime(currenttime);
            userInfoService.update(user);
//            map.put("code", "200");
//            map.put("message", "token过期，不过已经更新了");
////            map.put("data", "没有数据");
//            response.setContentType("application/json; charset=utf-8");
//            PrintWriter writer = response.getWriter();
//            writer.print(JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue,
//                    SerializerFeature.WriteDateUseDateFormat));
//            writer.close();
//            response.flushBuffer();
            return true;
        }


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
